package script.learn
{
	import laya.utils.Handler;
	
	import model.ConstantModel;
	import model.HttpErrorCode;
	import model.HttpRequestUtil;
	import model.lesson.ClassLevelVo;
	import model.lesson.LearnEnglishModel;
	
	import script.ViewManager;
	import script.baseView.BaseViewController;
	import script.learn.item.ClassLevelCell;
	
	import ui.learn.ChooseLevelPanelUI;
	
	public class ChooseClassLevelController extends BaseViewController
	{
		private var uiSkin:ChooseLevelPanelUI;
		
		private var param:Object;
		public function ChooseClassLevelController()
		{
			super();
		}
		
		override public function onStart():void
		{
			uiSkin = this.owner as ChooseLevelPanelUI;
			
			uiSkin.levellist.itemRender = ClassLevelCell;
			
			//uiSkin.orderList.vScrollBarSkin = "";
			uiSkin.levellist.repeatX = 1;
			uiSkin.levellist.spaceY = 20;
			
			uiSkin.levellist.renderHandler = new Handler(this, updatelevelList);
			uiSkin.levellist.selectHandler = new Handler(this,selectLevel);
			uiSkin.levellist.selectEnable = true;
			uiSkin.levellist.array = [];
			
			var postdata:String = "type=" + param;
			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.getClassLevel,this,onGetClasses,postdata,"post");

		}
		
		private function onGetClasses(data:*):void
		{
			if(this.destroyed)
				return;
			
			var result:Object = JSON.parse(data);
			if(result.code == HttpErrorCode.OK)
			{
				uiSkin.levellist.array = result.classlist;
			}
		}
		public function updatelevelList(cell:ClassLevelCell):void
		{
			cell.setData(cell.dataSource);
		}
		
		public function selectLevel(index:int):void
		{
			if(index < 0)
				return;
			if(param == ConstantModel.SUBJECT_ENGLISH)
			{
				LearnEnglishModel.instance.classlevel = new ClassLevelVo(uiSkin.levellist.array[index]);
				
				ViewManager.instance.openView(ViewManager.VIEW_CHOOSE_ENGLISH_LESSON_PANEL,false,uiSkin.levellist.array[index]);
				
				uiSkin.levellist.selectedIndex = -1;
			}
			else if(param == ConstantModel.SUBJECT_CHINESE)
			{
				//Lear.instance.classlevel = new ClassLevelVo(uiSkin.levellist.array[index]);
				
				ViewManager.instance.openView(ViewManager.VIEW_CHOOSE_CHINESE_LESSON_PANEL,false,uiSkin.levellist.array[index]);
				
				uiSkin.levellist.selectedIndex = -1;
			}
		}
	}
}